package com.d0325;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TestSimpleDateFormat {
    public static void main(String[] args) {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");

        Date date = new Date();//创建日期对象，得到当前时间
        //将日期对象进行格式化输出
        System.out.println(sdf1.format(date));
        System.out.println(sdf2.format(date));

        Calendar calendar = Calendar.getInstance();//创建日历对象，指定时间为 2017-3-21 10:52:24
        calendar.set(2020,Calendar.MONTH,31,10,42,52);
        //将日历对象用getTime方法转换成Date对象，使用format格式化
        System.out.println(sdf1.format(calendar.getTime()));
        System.out.println(sdf2.format(calendar.getTime()));

        //String==>Date
        String birth = "1999年03月28日";
        Date birDate = null;
        try {
            birDate = sdf1.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println(birDate);

        String birth2 = "1998/11/23 10:23:25";
        try {
            Date birDate2 = sdf2.parse(birth2);
            System.out.println(birDate2);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

}
